package com.sinodata.bsm.cicp.finder.windows.snmpfind;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;

import com.sinodata.bsm.cicp.util.snmp.GetData;
import com.sinodata.bsm.cicp.util.snmp.MibDiskAndMemoEntry;
import com.sinodata.bsm.cicp.util.snmp.MibProcessorEntry;
import com.sinodata.bsm.cicp.util.snmp.MibSystemEntry;
import com.sinodata.bsm.cicp.util.snmp.OMMappingInfo;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-8-26 下午2:37:12          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class OSFinder extends SnmpFinder {

    private static final Logger logger = Logger.getLogger(OSFinder.class);

    @Override
    public List<String[]> find(Snmp snmp, CommunityTarget target) {
        /**
         * 主机名
         */
        String sysName = null;

        /**
         * cpu核数
         */
        int cpuNumber = -1;

        /**
         * 物理内存页大小，单位B
         */
        long physicalMemoryPageSize = -1;

        /**
         * 物理内存页数
         */
        long physicalMemoryPageNumber = -1;

        /**
         * 物理内存大小，单位MB
         */
        long physicalMemorySizeMB = -1;

        /**
         * 厂商
         */
        String company = "Microsoft";

        /**
         * sysObjectId
         */
        String sysOId = null;

        /**
         * 主机描述
         */
        String sysDesc = null;
        try {
            List<OMMappingInfo> os = GetData.getAllTableData(snmp, MibSystemEntry.class, target);
            List<OMMappingInfo> cpu = GetData.getAllTableData(snmp, MibProcessorEntry.class, target);
            List<OMMappingInfo> memory = GetData.getAllTableData(snmp, MibDiskAndMemoEntry.class, target);

            if (os.size() < 1) {
                throw new Exception(ip + " snmp发现windows异常，没有获取到windows系统数据");
            }
            if (cpu.size() < 1) {
                throw new Exception(ip + " snmp发现windows异常，没有获取到windows cpu数据");
            }
            if (memory.size() < 1) {
                throw new Exception(ip + " snmp发现windows异常，没有获取到windows内存数据");
            }
            for (OMMappingInfo mapping : os) {
                MibSystemEntry system = (MibSystemEntry) mapping;
                sysName = system.getSysName();
                sysOId = system.getSysObjectID();
                sysDesc = system.getSysDescr();
                break;
            }
            cpuNumber = cpu.size();
            for (OMMappingInfo mapping : memory) {
                MibDiskAndMemoEntry mem = (MibDiskAndMemoEntry) mapping;
                if ("1.3.6.1.2.1.25.2.1.2".equals(mem.getHrStorageType())) {
                    physicalMemoryPageSize = mem.getHrStorageAllocationUnits();
                    physicalMemoryPageNumber = mem.getHrStorageSize();
                    break;
                }
            }
            if (physicalMemoryPageSize == -1) {
                throw new Exception(ip + " snmp 采集windows内存指标有其他oid，现在程序中使用的oid匹配不成功");
            }
            physicalMemorySizeMB = physicalMemoryPageSize * 1L * physicalMemoryPageNumber / 1024 / 1024;

            List<String[]> resultList = new ArrayList<String[]>();
            String[] resultArray = new String[10];
            resultList.add(resultArray);
            resultArray[0] = ip + "_" + sysName;
            resultArray[1] = "windows_" + ip;
            resultArray[2] = "windows_" + ip;
            resultArray[3] = sysName;
            resultArray[4] = cpuNumber + "";
            resultArray[5] = physicalMemoryPageSize + "";
            resultArray[6] = physicalMemorySizeMB + "";
            resultArray[7] = company;
            resultArray[8] = sysOId;
            resultArray[9] = sysDesc;
            return resultList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
